import {defineStore} from 'pinia'

interface ILoginJump {
    url?: string;
    type?: string;
}


export const useAdminStore = defineStore('admin', {
    state: () => {
        return {
            admin: {},//登陆者的身份
            bizShopCity: {},//登陆者的管理门店 - 城市
            bizShop: {},//登陆者的管理门店
            bizBrand: {},//登陆者的管理门店
            biz: [],//登陆者的管理门店
            brands: [],//登陆者的管理品牌
            rules: {},//登陆者的权限规则
            menus: [],//登陆者的管理部门
            bizDept: {},//登陆者的管理部门
            bizDeptEmployee: {},//登陆者的管理部门
        }
    },
    getters: {
        getAdmin(): Object {
            return this.admin
        },
        getRules(): Object {
            return this.rules
        },
        getBrands(): Object {
            return this.brands
        },
        getMenus(): Object {
            return this.menus
        },
        getBizDept(): Object {
            return this.bizDept
        },
        getBizShopCity(): Object {
            return this.bizShopCity
        },
        getBizShop(): Object {
            return this.bizShop
        },
        getBizBrand(): Object {
            return this.bizBrand
        },
        getBiz(): Array<Object> {
            return this.biz
        },
        getBizDeptEmployee():Object {
            return this.bizDeptEmployee
        },
    },
    actions: {
        setAdmin(res: Object): void {
            this.setMenus(res.menus)
            this.setRules(res.rules)
            this.setBizShop(res)
            this.setBrands(res.manage_brand)
            this.setBizBrand(res)
            this.setBiz(res)
            this.setBizDept(res.manage_dept_tree)
            this.setBizDeptEmployee(res.manage_dept_employee)
            delete res.menus
            delete res.rules
            delete res.manage_shop
            delete res.manage_brand
            delete res.manage_biz
            delete res.manage_dept_tree
            delete res.manage_dept_employee
            this.admin = res
        },
        setBizDept(res): void {
            this.bizDept = res
        },
        setBizShop(res): void {

            const shopBizCity = {}
            const shopBiz = {}

            for (let item of res.manage_shop) {

                // console.log(item)

                if (!shopBizCity.hasOwnProperty(item.biz_id)) {
                    shopBizCity[item.biz_id] = [];
                }

                if (!shopBizCity[item.biz_id].some(row => row.id == item.city)) {
                    shopBizCity[item.biz_id].push({
                        id: item.city,
                        name: item.city_name
                    })
                }

                if (!shopBiz.hasOwnProperty(item.biz_id)) {
                    shopBiz[item.biz_id] = [];
                }

                // if(!shopBiz[item.biz_id].some(row => row.id == item.id)){
                //     shopBiz[item.biz_id].push(item)
                // }

                shopBiz[item.biz_id].push(item)

            }

            this.bizShopCity = shopBizCity
            this.bizShop = shopBiz
        },
        setBizBrand(res): void {

            const brandBiz = {}

            for (let item of res.manage_biz) {

                if (!brandBiz.hasOwnProperty(item.id)) {
                    brandBiz[item.id] = item.mange_brands
                }
            }

            this.bizBrand = brandBiz
        },
        setBizDeptEmployee(res): void {
            this.bizDeptEmployee = res
        },
        setBiz(res): void {

            const biz = []

            for (let item of res.manage_biz) {
                biz.push({
                    id: item.id,
                    name: item.name,
                    logo: item.logo,
                    status: item.status,
                })

            }

            this.biz = biz
        },

        setRules(res: Object): void {
            this.rules = res
        },
        setMenus(res): void {
            this.menus = res
        },
        setBrands(res): void {
            this.brands = res
        },
        delAdmin() {
            this.admin = {}
            this.delRules()
            this.delMenus()
            this.delBizDept()
            this.delBizShop()
            this.delBrands()
            this.delBizBrand()
            this.delBiz()
            this.delBizDeptEmployee()
        },
        delBizShop() {
            this.bizShopCity = {}
            this.bizShop = {}
        },
        delBizBrand() {
            this.bizBrand = {}
        },
        delRules() {
            this.rules = {}
        },
        delMenus() {
            this.menus = []
        },
        delBizDept() {
            this.bizDept = {}
        },
        delBrands() {
            this.brands = []
        },
        delBiz() {
            this.biz = []
        },
        delBizDeptEmployee(){
            this.bizDeptEmployee = {}
        }
    },
    persist: true
})